package com.zx.yixun.ui

import android.os.Bundle
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zx.yixun.R
import com.zx.yixun.data.GetLineBean
import com.zx.yixun.ui.adapter.LineBottomAdapter
import com.zx.yixun.ui.adapter.LineTopAdapter
import com.zx.yixun.ui.adapter.RunLineBottomAdapter
import com.zx.yixun.ui.adapter.RunLineTopAdapter
import com.zx.ymy.base.BaseActivity
import com.zx.ymy.net.NetWorkHelper
import com.zx.ymy.net.api.MyApi
import kotlinx.android.synthetic.main.activity_maincontrol.*

class MainControlActivity :BaseActivity() {

    private var lineTopAdapter: LineTopAdapter? = null
    private var lineBottomAdapter: LineBottomAdapter? = null
    private var runLineTopAdapter: RunLineTopAdapter? = null
    private var runLineBottomAdapter: RunLineBottomAdapter? = null

    override fun getToolbar(): Toolbar? {
        return null
    }

    override fun getContentId(): Int {
        return R.layout.activity_maincontrol
    }

    override fun onContentCreate(savedInstanceState: Bundle?) {

        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = RecyclerView.HORIZONTAL
        recycleview1.layoutManager=linearLayoutManager

        val linearLayoutManager2 = LinearLayoutManager(this)
        linearLayoutManager2.orientation = RecyclerView.HORIZONTAL
        recycleview2.layoutManager=linearLayoutManager2

        val linearLayoutManager3 = LinearLayoutManager(this)
        linearLayoutManager3.orientation = RecyclerView.HORIZONTAL
        recycleview3.layoutManager=linearLayoutManager3

        val linearLayoutManager4 = LinearLayoutManager(this)
        linearLayoutManager4.orientation = RecyclerView.HORIZONTAL
        recycleview4.layoutManager=linearLayoutManager4

        lineTopAdapter = LineTopAdapter()
        lineBottomAdapter = LineBottomAdapter()
        runLineTopAdapter= RunLineTopAdapter()
        runLineBottomAdapter= RunLineBottomAdapter()
        recycleview1.adapter= lineTopAdapter
        recycleview2.adapter= runLineTopAdapter
        recycleview3.adapter= runLineBottomAdapter
        recycleview4.adapter= lineBottomAdapter


        recycleview1.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if (recyclerView.scrollState != RecyclerView.SCROLL_STATE_IDLE) {
                    recycleview2.scrollBy(dx, dy)
                    recycleview3.scrollBy(dx, dy)
                    recycleview4.scrollBy(dx, dy)
                }
            }
        })

        recycleview2.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if (recyclerView.scrollState != RecyclerView.SCROLL_STATE_IDLE) {
                    recycleview1.scrollBy(dx, dy)
                    recycleview3.scrollBy(dx, dy)
                    recycleview4.scrollBy(dx, dy)
                }
            }
        })

        recycleview3.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if (recyclerView.scrollState != RecyclerView.SCROLL_STATE_IDLE) {
                    recycleview2.scrollBy(dx, dy)
                    recycleview1.scrollBy(dx, dy)
                    recycleview4.scrollBy(dx, dy)
                }
            }
        })

        recycleview4.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if (recyclerView.scrollState != RecyclerView.SCROLL_STATE_IDLE) {
                    recycleview2.scrollBy(dx, dy)
                    recycleview3.scrollBy(dx, dy)
                    recycleview1.scrollBy(dx, dy)
                }
            }
        })


        GetLineDataThread().start()
    }


    fun getData(){
        NetWorkHelper.instance().createApi(MyApi::class.java).getLineData(GetLineBean("cae8dbc086694b5cb27a31fdca298814"))
            .runRxLoading({
                it?.let {
                    val topList = it.subList(0, it.size / 2)
                    val bottomList = it.subList(it.size / 2, it.size).reversed()
                    lineTopAdapter?.setNewData(topList)
                    lineBottomAdapter?.setNewData(bottomList)
                    runLineTopAdapter?.setNewData(topList)
                    runLineBottomAdapter?.setNewData(bottomList)
                }
            })
    }


    // 读取数据
    inner class GetLineDataThread : Thread() {
        override fun run() {
            while (true) {
                try {
                    this@MainControlActivity.getData()
                    sleep(5000)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
            }
        }
    }


}